package com.jh.paymentchannel.service;

import com.jh.paymentchannel.business.BranchbankBussiness;
import com.jh.paymentchannel.business.TopupPayChannelBusiness;
import com.jh.paymentchannel.pojo.CJQuickBindCard;
import com.jh.paymentchannel.pojo.CJRegister;
import com.jh.paymentchannel.util.Util;
import com.jh.paymentchannel.util.cj.BaseResMessage;
import com.jh.paymentchannel.util.cj.Constants;
import com.jh.paymentchannel.util.cj.EncryptUtil;
import com.jh.paymentchannel.util.cj.PayGetToken;
import com.jh.paymentchannel.util.cj.SignUtil;
import com.jh.paymentchannel.util.cj.TokenConstants;
import com.jh.paymentchannel.util.cj.TokenRes;
import net.sf.json.JSONObject;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeUnit;

@Service
public class CJQuickTopupRequest implements TopupRequest {

	private static final Logger log = LoggerFactory.getLogger(CJQuickTopupRequest.class);

	@Autowired
	private TopupPayChannelBusiness topupPayChannelBusiness;

	@Autowired
	private BranchbankBussiness branchbankBussiness;

	@Autowired
	private Util util;

	@Value("${payment.ipAddress}")
	private String ipAddress;
	// 服务商编号
	@Value("${cj.org_no}")
	private String serverProviderCode;
	// 服务商秘钥
	@Value("${cj.key}")
	private String Key;

	private String Url = "https://qkapi.chanpay.com";

	private static final Charset UTF_8 = StandardCharsets.UTF_8;

	public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

	private String rateCode = "1000002";

	private String capAmount = "999999";

	@Override
	public Map<String, String> topupRequest(HttpServletRequest request, String amount, String ordercode,
			String orderdesc, String extra, String notifyurl, String returnurl, String channelParam) throws Exception {
		log.info("ipAddress=====" + ipAddress);

		Map<String, String> map = new HashMap<String, String>();
		RestTemplate restTemplate = new RestTemplate();
		URI uri = util.getServiceUrl("transactionclear", "error url request!");
		String url = uri.toString() + "/v1.0/transactionclear/payment/query/ordercode";
		MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("order_code", ordercode);
		String result = restTemplate.postForObject(url, requestEntity, String.class);
		log.info("RESULT================" + result);
		JSONObject jsonObject;
		JSONObject resultObj;
		try {
			jsonObject = JSONObject.fromObject(result);
			resultObj = jsonObject.getJSONObject("result");
		} catch (Exception e) {
			log.error("查询订单信息出错");
			map.put("resp_code", "failed");
			map.put("channel_type", "jf");
			map.put("resp_message", "没有该订单信息");
			return map;
		}
		
		String userid = resultObj.getString("userid");
		// 费率
		String rate = resultObj.getString("rate");
		// 额外费率
		String extraFee = resultObj.getString("extraFee");
		// 银行卡
		String bankCard = resultObj.getString("bankcard");

		restTemplate = new RestTemplate();
		uri = util.getServiceUrl("user", "error url request!");
		url = uri.toString() + "/v1.0/user/bank/default/userid";
		requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("user_id", userid);
		result = restTemplate.postForObject(url, requestEntity, String.class);

		log.info("RESULT================" + result);
		try {
			jsonObject = JSONObject.fromObject(result);
			resultObj = jsonObject.getJSONObject("result");
		} catch (Exception e) {
			log.error("查询默认结算卡出错");
			map.put("resp_code", "failed");
			map.put("channel_type", "jf");
			map.put("resp_message", "查询默认结算卡有误");
			return map;
		}

		if (resultObj.isNullObject()) {

			map.put("resp_code", "failed");
			map.put("channel_type", "jf");
			map.put("resp_message", "没有结算卡");
			return map;
		}

		String cardNo = resultObj.getString("cardNo");
		String userName = resultObj.getString("userName");
		/** 身份证号 */
		String idcard = resultObj.getString("idcard");
		String phone = resultObj.getString("phone");
		// 开户行所在省
		String provinceOfBank = resultObj.getString("province");

		System.out.println("provinceOfBank" + provinceOfBank);
		// 开户行所在市
		String cityOfBank = resultObj.getString("city");

		System.out.println("cityOfBank" + cityOfBank);
		// 银行名称
		String bankName = resultObj.getString("bankName");
		// 支行名称
		String bankBranchName = resultObj.getString("bankBranchName");

		String cardType = resultObj.getString("cardType");

		String bankBranchNo;
		try {
			bankBranchNo = branchbankBussiness.getNumByName(bankBranchName);
		} catch (Exception e1) {
			map.put("resp_code", "failed");
			map.put("channel_type", "jf");
			map.put("resp_message", "查询支行联行号有误");
			return map;
		}
		System.out.println("bankBranchNo" + bankBranchNo);

		restTemplate = new RestTemplate();
		uri = util.getServiceUrl("user", "error url request!");
		url = uri.toString() + "/v1.0/user/bank/default/cardno";
		requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("cardno", bankCard);
		requestEntity.add("type", "0");
		result = restTemplate.postForObject(url, requestEntity, String.class);
		log.info("接口/v1.0/user/bank/default/cardno--RESULT================" + result);
		try {
			jsonObject = JSONObject.fromObject(result);
			resultObj = jsonObject.getJSONObject("result");
		} catch (Exception e) {
			log.error("查询银行卡信息出错");
			map.put("resp_code", "failed");
			map.put("channel_type", "jf");
			map.put("resp_message", "查询银行卡信息有误");
			return map;
		}
		

		String cardName = resultObj.getString("bankName");
		String expiredTime = resultObj.getString("expiredTime");

		String securityCode = resultObj.getString("securityCode");

		String cardtype = resultObj.getString("cardType");

		String bankname = Util.queryBankNameByBranchName(bankName);
		String cardname = Util.queryBankNameByBranchName(cardName);


		CJRegister cjRegister;
		try {
			cjRegister = topupPayChannelBusiness.getCJRegister(Long.parseLong(userid));
		} catch (Exception e) {
			map.put("resp_code", "failed");
			map.put("channel_type", "jf");
			map.put("resp_message", "查询用户进件信息有误");
			return map;
		}

		if (provinceOfBank == null || "".equals(provinceOfBank) || "null".equals(provinceOfBank)) {
			provinceOfBank = "";
		}

		if (cityOfBank == null || "".equals(cityOfBank) || "null".equals(cityOfBank)) {
			cityOfBank = "";
		}

		if (bankBranchNo == null || "".equals(bankBranchNo) || "".equals(bankBranchNo)) {
			bankBranchNo = "";
		}

		if (bankBranchName == null || "".equals(bankBranchName) || "null".equals(bankBranchName)) {
			bankBranchName = "";
		}

		if (expiredTime == null || "".equals(expiredTime) || "null".equals(expiredTime)) {
			expiredTime = "";
		}

		if (securityCode == null || "".equals(securityCode) || "null".equals(securityCode)) {
			securityCode = "";
		}

		if (cjRegister == null) {
			log.info("用户需要进件========");
			// cjRegister为空表示用户需要进件
			Map<String,String> maps = new HashMap<String, String>();
			maps.put("resp_code", "success");
			maps.put("channel_type", "jf");
			maps.put("redirect_url",
					ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
							+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo + "&bankNo="
							+ cardNo + "&bankCard=" + bankCard + "&cardName=" + URLEncoder.encode(cardname, "UTF-8")
							+ "&amount=" + amount + "&ordercode=" + ordercode + "&provinceOfBank="
							+ URLEncoder.encode(provinceOfBank, "UTF-8") + "&cityOfBank="
							+ URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
							+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
							+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype=" + URLEncoder.encode(cardtype, "UTF-8")
							+ "&expiredTime=" + expiredTime + "&securityCode=" + securityCode + "&isRegister=0");
			return maps;
		}

		CJQuickBindCard cjQuickBindCard = topupPayChannelBusiness.getCJQuickBindCard(userid, bankCard);

		CJRegister cjRegisterByRateCode = topupPayChannelBusiness.getCJRegisterByRateCode2(Long.parseLong(userid),
				rateCode);

		if (!extraFee.equals(cjRegister.getWithdrawDepositsingleFee())) {
			boolean updateCJQuickWithdrawRate = updateCJQuickWithdrawRate(ordercode);
			log.info("用户额外费用需要修改========");
			if(updateCJQuickWithdrawRate){
				if (cjRegisterByRateCode == null) {
					boolean addCJRateCode = addCJQuickRateCode(ordercode);
					log.info("商户费率编号需要新增========");
					if (addCJRateCode) {
						if (!rate.equals(cjRegister.getCreditRate())) {
							log.info("用户费率变更需要更改========");
							boolean updateRate = updateCJQuickRate(ordercode);
							if (updateRate) {
								if (cjQuickBindCard == null) {
									log.info("用户需要绑卡========");
									// cjBankCard为空表示用户需要绑卡
									map.put("resp_code", "success");
									map.put("channel_type", "jf");
									map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
											+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
											+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
											+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
											+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
											+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
											+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
											+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
											+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
											+ "&securityCode=" + securityCode + "&isRegister=1");
									return map;
								} else if (!cardNo.equals(cjRegister.getBankCard())) {
									log.info("用户结算卡变更需要更改========");
									map.put("resp_code", "success");
									map.put("channel_type", "jf");
									map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
											+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
											+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
											+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
											+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
											+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
											+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
											+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
											+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
											+ "&securityCode=" + securityCode + "&isRegister=3");
									return map;

								} else {
									log.info("发起支付======");
									map.put("resp_code", "success");
									map.put("channel_type", "jf");
									map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
											+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
											+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
											+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
											+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
											+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
											+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
											+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
											+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
											+ "&securityCode=" + securityCode + "&isRegister=2");
									return map;
								}
							}else {
								map.put("resp_code", "failed");
								map.put("channel_type", "jf");
								map.put("resp_message", "亲，网络出错啦");
								return map;
							}
						//费率不需要修改
						}else {
							if (cjQuickBindCard == null) {
								log.info("用户需要绑卡========");
								// cjBankCard为空表示用户需要绑卡
								map.put("resp_code", "success");
								map.put("channel_type", "jf");
								map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
										+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
										+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
										+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
										+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
										+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
										+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
										+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
										+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
										+ "&securityCode=" + securityCode + "&isRegister=1");
								return map;
							} else if (!cardNo.equals(cjRegister.getBankCard())) {
								log.info("用户结算卡变更需要更改========");
								map.put("resp_code", "success");
								map.put("channel_type", "jf");
								map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
										+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
										+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
										+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
										+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
										+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
										+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
										+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
										+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
										+ "&securityCode=" + securityCode + "&isRegister=3");
								return map;

							} else {
								log.info("发起支付======");
								map.put("resp_code", "success");
								map.put("channel_type", "jf");
								map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
										+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
										+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
										+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
										+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
										+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
										+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
										+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
										+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
										+ "&securityCode=" + securityCode + "&isRegister=2");
								return map;
							}
						}

					}else {
						map.put("resp_code", "failed");
						map.put("channel_type", "jf");
						map.put("resp_message", "亲，网络出错啦");
						return map;
					}
				//商户费率不需要新增
				} else {
					if (!rate.equals(cjRegister.getCreditRate())) {
						log.info("用户费率变更需要更改========");
						boolean updateRate = updateCJQuickRate(ordercode);
						if (updateRate) {
							if (cjQuickBindCard == null) {
								log.info("用户需要绑卡========");
								// cjBankCard为空表示用户需要绑卡
								map.put("resp_code", "success");
								map.put("channel_type", "jf");
								map.put("redirect_url",
										ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
												+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
												+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
												+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
												+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
												+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
												+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
												+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
												+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
												+ "&securityCode=" + securityCode + "&isRegister=1");
								return map;
							} else if (!cardNo.equals(cjRegister.getBankCard())) {
								log.info("用户结算卡变更需要更改========");
								map.put("resp_code", "success");
								map.put("channel_type", "jf");
								map.put("redirect_url",
										ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
												+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
												+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
												+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
												+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
												+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
												+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
												+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
												+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
												+ "&securityCode=" + securityCode + "&isRegister=3");
								return map;

							} else {
								log.info("发起支付======");
								map.put("resp_code", "success");
								map.put("channel_type", "jf");
								map.put("redirect_url",
										ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
												+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
												+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
												+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
												+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
												+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
												+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
												+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
												+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
												+ "&securityCode=" + securityCode + "&isRegister=2");
								return map;
							}
						}else {
							map.put("resp_code", "failed");
							map.put("channel_type", "jf");
							map.put("resp_message", "亲，网络出错啦");
							return map;
						}
					}else {
						if (cjQuickBindCard == null) {
							log.info("用户需要绑卡========");
							// cjBankCard为空表示用户需要绑卡
							map.put("resp_code", "success");
							map.put("channel_type", "jf");
							map.put("redirect_url",
									ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
											+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
											+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
											+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
											+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
											+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
											+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
											+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
											+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
											+ "&securityCode=" + securityCode + "&isRegister=1");
							return map;
						} else if (!cardNo.equals(cjRegister.getBankCard())) {
							log.info("用户结算卡变更需要更改========");
							map.put("resp_code", "success");
							map.put("channel_type", "jf");
							map.put("redirect_url",
									ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
											+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
											+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
											+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
											+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
											+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
											+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
											+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
											+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
											+ "&securityCode=" + securityCode + "&isRegister=3");
							return map;

						} else {
							log.info("发起支付======");
							map.put("resp_code", "success");
							map.put("channel_type", "jf");
							map.put("redirect_url",
									ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
											+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
											+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
											+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
											+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
											+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
											+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
											+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
											+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
											+ "&securityCode=" + securityCode + "&isRegister=2");
							return map;
						}
					}
					
				}
			}else{
				map.put("resp_code", "failed");
				map.put("channel_type", "jf");
				map.put("resp_message", "亲，网络出错啦");
				return map;
			}
			
		//额外费用不需要修改	
		}else{
			
			if (cjRegisterByRateCode == null) {
				boolean addCJRateCode = addCJQuickRateCode(ordercode);
				log.info("商户费率编号需要新增========");
				if (addCJRateCode) {
					if (!rate.equals(cjRegister.getCreditRate())) {
						log.info("用户费率变更需要更改========");
						boolean updateRate = updateCJQuickRate(ordercode);
						if (updateRate) {
							if (cjQuickBindCard == null) {
								log.info("用户需要绑卡========");
								// cjBankCard为空表示用户需要绑卡
								map.put("resp_code", "success");
								map.put("channel_type", "jf");
								map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
										+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
										+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
										+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
										+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
										+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
										+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
										+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
										+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
										+ "&securityCode=" + securityCode + "&isRegister=1");
								return map;
							} else if (!cardNo.equals(cjRegister.getBankCard())) {
								log.info("用户结算卡变更需要更改========");
								map.put("resp_code", "success");
								map.put("channel_type", "jf");
								map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
										+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
										+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
										+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
										+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
										+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
										+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
										+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
										+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
										+ "&securityCode=" + securityCode + "&isRegister=3");
								return map;

							} else {
								log.info("发起支付======");
								map.put("resp_code", "success");
								map.put("channel_type", "jf");
								map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
										+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
										+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
										+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
										+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
										+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
										+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
										+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
										+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
										+ "&securityCode=" + securityCode + "&isRegister=2");
								return map;
							}
						}else{
							
							map.put("resp_code", "failed");
							map.put("channel_type", "jf");
							map.put("resp_message", "亲，网络出错啦");
							return map;
						}
						
					}else {
						if (cjQuickBindCard == null) {
							log.info("用户需要绑卡========");
							// cjBankCard为空表示用户需要绑卡
							map.put("resp_code", "success");
							map.put("channel_type", "jf");
							map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
									+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
									+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
									+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
									+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
									+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
									+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
									+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
									+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
									+ "&securityCode=" + securityCode + "&isRegister=1");
							return map;
						} else if (!cardNo.equals(cjRegister.getBankCard())) {
							log.info("用户结算卡变更需要更改========");
							map.put("resp_code", "success");
							map.put("channel_type", "jf");
							map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
									+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
									+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
									+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
									+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
									+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
									+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
									+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
									+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
									+ "&securityCode=" + securityCode + "&isRegister=3");
							return map;

						} else {
							log.info("发起支付======");
							map.put("resp_code", "success");
							map.put("channel_type", "jf");
							map.put("redirect_url", ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
									+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
									+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
									+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
									+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
									+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
									+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
									+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
									+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
									+ "&securityCode=" + securityCode + "&isRegister=2");
							return map;
						}
					}

				}else {
					map.put("resp_code", "failed");
					map.put("channel_type", "jf");
					map.put("resp_message", "亲，网络出错啦");
					return map;
				}
				
			} else {

				if (!rate.equals(cjRegister.getCreditRate())) {
					log.info("用户费率变更需要更改========");
					boolean updateRate = updateCJQuickRate(ordercode);
					if (updateRate) {
						if (cjQuickBindCard == null) {
							log.info("用户需要绑卡========");
							// cjBankCard为空表示用户需要绑卡
							map.put("resp_code", "success");
							map.put("channel_type", "jf");
							map.put("redirect_url",
									ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
											+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
											+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
											+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
											+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
											+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
											+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
											+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
											+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
											+ "&securityCode=" + securityCode + "&isRegister=1");
							return map;
						} else if (!cardNo.equals(cjRegister.getBankCard())) {
							log.info("用户结算卡变更需要更改========");
							map.put("resp_code", "success");
							map.put("channel_type", "jf");
							map.put("redirect_url",
									ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
											+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
											+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
											+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
											+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
											+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
											+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
											+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
											+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
											+ "&securityCode=" + securityCode + "&isRegister=3");
							return map;

						} else {
							log.info("发起支付======");
							map.put("resp_code", "success");
							map.put("channel_type", "jf");
							map.put("redirect_url",
									ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
											+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
											+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
											+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
											+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
											+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
											+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
											+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
											+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
											+ "&securityCode=" + securityCode + "&isRegister=2");
							return map;
						}
					}else {
						map.put("resp_code", "failed");
						map.put("channel_type", "jf");
						map.put("resp_message", "亲，网络出错啦");
						return map;
							
					}
				}else {
					
					if (cjQuickBindCard == null) {
						log.info("用户需要绑卡========");
						map.put("resp_code", "success");
						map.put("channel_type", "jf");
						map.put("redirect_url",
								ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
										+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
										+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
										+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
										+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
										+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
										+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
										+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
										+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
										+ "&securityCode=" + securityCode + "&isRegister=1");
						return map;
					} else if (!cardNo.equals(cjRegister.getBankCard())) {
						log.info("用户结算卡变更需要更改========");
						map.put("resp_code", "success");
						map.put("channel_type", "jf");
						map.put("redirect_url",
								ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
										+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
										+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
										+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
										+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
										+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
										+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
										+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
										+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
										+ "&securityCode=" + securityCode + "&isRegister=3");
						return map;

					} else {
						log.info("发起支付======");
						map.put("resp_code", "success");
						map.put("channel_type", "jf");
						map.put("redirect_url",
								ipAddress + "/v1.0/paymentchannel/topup/tocjquickbankinfo?bankName="
										+ URLEncoder.encode(bankname, "UTF-8") + "&bankBranchId=" + bankBranchNo
										+ "&bankNo=" + cardNo + "&bankCard=" + bankCard + "&cardName="
										+ URLEncoder.encode(cardname, "UTF-8") + "&amount=" + amount + "&ordercode="
										+ ordercode + "&provinceOfBank=" + URLEncoder.encode(provinceOfBank, "UTF-8")
										+ "&cityOfBank=" + URLEncoder.encode(cityOfBank, "UTF-8") + "&bankBranchName="
										+ URLEncoder.encode(bankBranchName, "UTF-8") + "&cardType="
										+ URLEncoder.encode(cardType, "UTF-8") + "&cardtype="
										+ URLEncoder.encode(cardtype, "UTF-8") + "&expiredTime=" + expiredTime
										+ "&securityCode=" + securityCode + "&isRegister=2");
						return map;
					}
				}
			}
		}

		

	}

	// 修改费率
	public boolean updateCJQuickRate(String ordercode) throws Exception {
		
		log.info("进入修改费率接口=======");
		Map map = new HashMap();
		Map<String, String> maps = new HashMap<String, String>();
		RestTemplate restTemplate = new RestTemplate();
		URI uri = util.getServiceUrl("transactionclear", "error url request!");
		String url = uri.toString() + "/v1.0/transactionclear/payment/query/ordercode";
		MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("order_code", ordercode);
		String result = restTemplate.postForObject(url, requestEntity, String.class);
		log.info("RESULT================" + result);
		JSONObject jsonObject = JSONObject.fromObject(result);
		JSONObject resultObj = jsonObject.getJSONObject("result");

		String userid = resultObj.getString("userid");
		// 费率
		String rate = resultObj.getString("rate");

		// 通过 userId 查询用户进件信息
		CJRegister cjRegister = null;
		try {
			cjRegister = topupPayChannelBusiness.getCJRegister(Long.parseLong(userid));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		String merchantCode = cjRegister.getMerchantCode();

		String CapAmount = new BigDecimal(capAmount).multiply(new BigDecimal("100")).setScale(0).toString();

		// 获取令牌
		BaseResMessage<TokenRes> tokenRes = new PayGetToken().token(Key, TokenConstants.MERCHANT_CHANGE_TOKEN,
				serverProviderCode);
		String token = tokenRes.getData().getToken();
		// 解密令牌
		String tokenText = EncryptUtil.desDecrypt(token, Key);

		// 构建签名参数
		TreeMap<String, Object> signParams = new TreeMap<String, Object>();
		signParams.put("token", tokenText);
		signParams.put("serverProviderCode", serverProviderCode);
		signParams.put("merchantCode", merchantCode);
		signParams.put("changeType", "1");
		signParams.put("bankAccountNo", "");
		signParams.put("phoneno", "");
		signParams.put("bankName", "");
		signParams.put("bankSubName", "");
		signParams.put("bankCode", "");
		signParams.put("bankAbbr", "");
		signParams.put("bankChannelNo", "");
		signParams.put("bankProvince", "");
		signParams.put("bankCity", "");
		signParams.put("rateCode", rateCode);
		signParams.put("debitRate", rate);
		signParams.put("debitCapAmount", CapAmount);
		signParams.put("creditRate", rate);
		signParams.put("creditCapAmount", CapAmount);
		signParams.put("withdrawDepositRate", "");
		signParams.put("withdrawDepositSingleFee", "");

		// 构建请求参数
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("token", tokenText);
		jsonObj.put("serverProviderCode", serverProviderCode);
		jsonObj.put("merchantCode", merchantCode);
		jsonObj.put("changeType", "1");
		jsonObj.put("bankAccountNo", "");
		jsonObj.put("phoneno", "");
		jsonObj.put("bankName", "");
		jsonObj.put("bankSubName", "");
		jsonObj.put("bankCode", "");
		jsonObj.put("bankAbbr", "");
		jsonObj.put("bankChannelNo", "");
		jsonObj.put("bankProvince", "");
		jsonObj.put("bankCity", "");
		jsonObj.put("rateCode", rateCode);
		jsonObj.put("debitRate", rate);
		jsonObj.put("debitCapAmount", CapAmount);
		jsonObj.put("creditRate", rate);
		jsonObj.put("creditCapAmount", CapAmount);
		jsonObj.put("withdrawDepositRate", "");
		jsonObj.put("withdrawDepositSingleFee", "");
		jsonObj.put("sign", SignUtil.signByMap(Key, signParams));

		// 接口访问
		String jsonReq = jsonObj.toString();
		log.info("jsonReq: " + jsonReq);

		OkHttpClient client = new OkHttpClient();
		client.newBuilder().connectTimeout(Constants.CONNECT_TIMEOUT, TimeUnit.SECONDS)
				.writeTimeout(Constants.WRITE_TIMEOUT, TimeUnit.SECONDS)
				.readTimeout(Constants.READ_TIMEOUT, TimeUnit.SECONDS);
		RequestBody body = RequestBody.create(JSON, jsonReq);
		Request requests = new Request.Builder().url(Url + "/v1/merchant/merchantChange").post(body).build();
		Response responses = client.newCall(requests).execute();

		String jsonRsp = responses.body().string();
		log.info("jsonRsp: " + jsonRsp);

		boolean istrue = false;
		if (responses.isSuccessful()) {
			log.info("\n接口响应内容：" + jsonRsp);
			
			JSONObject fromObject = JSONObject.fromObject(jsonRsp);
			log.info("hehehe====="+fromObject.getString("code"));
			if("00".equals(fromObject.getString("code"))){
			CJRegister cjRegister1 = topupPayChannelBusiness.getCJRegister(Long.parseLong(userid));
			cjRegister1.setCreditRate(rate);

			topupPayChannelBusiness.createCJRegister(cjRegister1);

			istrue = true;
			
			}
		} else {
			log.info("响应码: " + responses.code());
			throw new IOException("Unexpected code " + responses.message());
		}

		return istrue;

	}

	// 新增商户费率编号
	public boolean addCJQuickRateCode(String ordercode) throws Exception {
		log.info("进入新增商户费率编号接口=======");
		Map map = new HashMap();
		Map<String, String> maps = new HashMap<String, String>();
		RestTemplate restTemplate = new RestTemplate();
		URI uri = util.getServiceUrl("transactionclear", "error url request!");
		String url = uri.toString() + "/v1.0/transactionclear/payment/query/ordercode";
		MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("order_code", ordercode);
		String result = restTemplate.postForObject(url, requestEntity, String.class);
		log.info("RESULT================" + result);
		JSONObject jsonObject = JSONObject.fromObject(result);
		JSONObject resultObj = jsonObject.getJSONObject("result");

		String userid = resultObj.getString("userid");
		// 费率
		String rate = resultObj.getString("rate");

		// 通过 userId 查询用户进件信息
		CJRegister cjRegister = null;
		try {
			cjRegister = topupPayChannelBusiness.getCJRegister(Long.parseLong(userid));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		String merchantCode = cjRegister.getMerchantCode();

		String CapAmount = new BigDecimal(capAmount).multiply(new BigDecimal("100")).setScale(0).toString();

		// 获取令牌
		BaseResMessage<TokenRes> tokenRes = new PayGetToken().token(Key, TokenConstants.MERCHANT_CHANGE_TOKEN,
				serverProviderCode);
		String token = tokenRes.getData().getToken();
		// 解密令牌
		String tokenText = EncryptUtil.desDecrypt(token, Key);

		// 构建签名参数
		TreeMap<String, Object> signParams = new TreeMap<String, Object>();
		signParams.put("token", tokenText);
		signParams.put("serverProviderCode", serverProviderCode);
		signParams.put("merchantCode", merchantCode);
		signParams.put("changeType", "3");
		signParams.put("bankAccountNo", "");
		signParams.put("phoneno", "");
		signParams.put("bankName", "");
		signParams.put("bankSubName", "");
		signParams.put("bankCode", "");
		signParams.put("bankAbbr", "");
		signParams.put("bankChannelNo", "");
		signParams.put("bankProvince", "");
		signParams.put("bankCity", "");
		signParams.put("rateCode", rateCode);
		signParams.put("debitRate", rate);
		signParams.put("debitCapAmount", CapAmount);
		signParams.put("creditRate", rate);
		signParams.put("creditCapAmount", CapAmount);
		signParams.put("withdrawDepositRate", "");
		signParams.put("withdrawDepositSingleFee", "");

		// 构建请求参数
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("token", tokenText);
		jsonObj.put("serverProviderCode", serverProviderCode);
		jsonObj.put("merchantCode", merchantCode);
		jsonObj.put("changeType", "3");
		jsonObj.put("bankAccountNo", "");
		jsonObj.put("phoneno", "");
		jsonObj.put("bankName", "");
		jsonObj.put("bankSubName", "");
		jsonObj.put("bankCode", "");
		jsonObj.put("bankAbbr", "");
		jsonObj.put("bankChannelNo", "");
		jsonObj.put("bankProvince", "");
		jsonObj.put("bankCity", "");
		jsonObj.put("rateCode", rateCode);
		jsonObj.put("debitRate", rate);
		jsonObj.put("debitCapAmount", CapAmount);
		jsonObj.put("creditRate", rate);
		jsonObj.put("creditCapAmount", CapAmount);
		jsonObj.put("withdrawDepositRate", "");
		jsonObj.put("withdrawDepositSingleFee", "");
		jsonObj.put("sign", SignUtil.signByMap(Key, signParams));

		// 接口访问
		String jsonReq = jsonObj.toString();
		log.info("jsonReq: " + jsonReq);

		OkHttpClient client = new OkHttpClient();
		client.newBuilder().connectTimeout(Constants.CONNECT_TIMEOUT, TimeUnit.SECONDS)
				.writeTimeout(Constants.WRITE_TIMEOUT, TimeUnit.SECONDS)
				.readTimeout(Constants.READ_TIMEOUT, TimeUnit.SECONDS);
		RequestBody body = RequestBody.create(JSON, jsonReq);
		Request requests = new Request.Builder().url(Url + "/v1/merchant/merchantChange").post(body).build();
		Response responses = client.newCall(requests).execute();

		String jsonRsp = responses.body().string();
		log.info("jsonRsp: " + jsonRsp);

		boolean istrue = false;
		if (responses.isSuccessful()) {
			log.info("\n接口响应内容：" + jsonRsp);
			
			JSONObject fromObject = JSONObject.fromObject(jsonRsp);
			log.info("hehehe====="+fromObject.getString("code"));
			if("00".equals(fromObject.getString("code"))){
				
				CJRegister cjRegister1 = topupPayChannelBusiness.getCJRegister(Long.parseLong(userid));
				cjRegister1.setRateCode2(rateCode);
				cjRegister1.setCreditRate(rate);

				topupPayChannelBusiness.createCJRegister(cjRegister1);

				istrue = true;
			}
			
		} else {
			log.info("响应码: " + responses.code());
			throw new IOException("Unexpected code " + responses.message());
		}

		return istrue;

	}

	// 修改每笔固定手续费
	public boolean updateCJQuickWithdrawRate(String ordercode) throws Exception {
		log.info("进入修改每笔固定手续费接口=======");
		Map map = new HashMap();
		Map<String, String> maps = new HashMap<String, String>();
		RestTemplate restTemplate = new RestTemplate();
		URI uri = util.getServiceUrl("transactionclear", "error url request!");
		String url = uri.toString() + "/v1.0/transactionclear/payment/query/ordercode";
		MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<String, String>();
		requestEntity.add("order_code", ordercode);
		String result = restTemplate.postForObject(url, requestEntity, String.class);
		log.info("RESULT================" + result);
		JSONObject jsonObject = JSONObject.fromObject(result);
		JSONObject resultObj = jsonObject.getJSONObject("result");

		String userid = resultObj.getString("userid");
		// 费率
		String rate = resultObj.getString("rate");
		// 额外费率
		String extraFee = resultObj.getString("extraFee");

		// 通过 userId 查询用户进件信息
		CJRegister cjRegister = null;
		try {
			cjRegister = topupPayChannelBusiness.getCJRegister(Long.parseLong(userid));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		String merchantCode = cjRegister.getMerchantCode();

		String ExtraFee = new BigDecimal(extraFee).multiply(new BigDecimal("100")).setScale(0).toString();
		
		// 获取令牌
		BaseResMessage<TokenRes> tokenRes = new PayGetToken().token(Key, TokenConstants.MERCHANT_CHANGE_TOKEN,
				serverProviderCode);
		String token = tokenRes.getData().getToken();
		// 解密令牌
		String tokenText = EncryptUtil.desDecrypt(token, Key);

		// 构建签名参数
		TreeMap<String, Object> signParams = new TreeMap<String, Object>();
		signParams.put("token", tokenText);
		signParams.put("serverProviderCode", serverProviderCode);
		signParams.put("merchantCode", merchantCode);
		signParams.put("changeType", "4");
		signParams.put("bankAccountNo", "");
		signParams.put("phoneno", "");
		signParams.put("bankName", "");
		signParams.put("bankSubName", "");
		signParams.put("bankCode", "");
		signParams.put("bankAbbr", "");
		signParams.put("bankChannelNo", "");
		signParams.put("bankProvince", "");
		signParams.put("bankCity", "");
		signParams.put("rateCode", rateCode);
		signParams.put("debitRate", "");
		signParams.put("debitCapAmount", "");
		signParams.put("creditRate", "");
		signParams.put("creditCapAmount", "");
		signParams.put("withdrawDepositRate", "0.0002");
		signParams.put("withdrawDepositSingleFee", ExtraFee);

		// 构建请求参数
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("token", tokenText);
		jsonObj.put("serverProviderCode", serverProviderCode);
		jsonObj.put("merchantCode", merchantCode);
		jsonObj.put("changeType", "4");
		jsonObj.put("bankAccountNo", "");
		jsonObj.put("phoneno", "");
		jsonObj.put("bankName", "");
		jsonObj.put("bankSubName", "");
		jsonObj.put("bankCode", "");
		jsonObj.put("bankAbbr", "");
		jsonObj.put("bankChannelNo", "");
		jsonObj.put("bankProvince", "");
		jsonObj.put("bankCity", "");
		jsonObj.put("rateCode", rateCode);
		jsonObj.put("debitRate", "");
		jsonObj.put("debitCapAmount", "");
		jsonObj.put("creditRate", "");
		jsonObj.put("creditCapAmount", "");
		jsonObj.put("withdrawDepositRate", "0.0002");
		jsonObj.put("withdrawDepositSingleFee", ExtraFee);
		jsonObj.put("sign", SignUtil.signByMap(Key, signParams));

		// 接口访问
		String jsonReq = jsonObj.toString();
		log.info("jsonReq: " + jsonReq);

		OkHttpClient client = new OkHttpClient();
		client.newBuilder().connectTimeout(Constants.CONNECT_TIMEOUT, TimeUnit.SECONDS)
				.writeTimeout(Constants.WRITE_TIMEOUT, TimeUnit.SECONDS)
				.readTimeout(Constants.READ_TIMEOUT, TimeUnit.SECONDS);
		RequestBody body = RequestBody.create(JSON, jsonReq);
		Request requests = new Request.Builder().url(Url + "/v1/merchant/merchantChange").post(body).build();
		Response responses = client.newCall(requests).execute();

		String jsonRsp = responses.body().string();
		log.info("jsonRsp: " + jsonRsp);

		boolean istrue = false;
		if (responses.isSuccessful()) {
			log.info("\n接口响应内容：" + jsonRsp);
			
			JSONObject fromObject = JSONObject.fromObject(jsonRsp);
			log.info("hahaha====="+fromObject.getString("code"));
			if("00".equals(fromObject.getString("code"))){
				CJRegister cjRegister1 = topupPayChannelBusiness.getCJRegister(Long.parseLong(userid));
				cjRegister1.setWithdrawDepositsingleFee(extraFee);

				topupPayChannelBusiness.createCJRegister(cjRegister1);

				istrue = true;
			}
			
		} else {
			log.info("响应码: " + responses.code());
			throw new IOException("Unexpected code " + responses.message());
		}

		return istrue;

	}

}
